package com.example.mybaiduweishi.activity;

import android.app.FragmentManager;
import android.content.SharedPreferences;
import android.graphics.drawable.AnimationDrawable;
import android.os.Bundle;
import android.support.v7.app.ActionBar;
import android.support.v7.widget.Toolbar;
import android.widget.FrameLayout;
import android.widget.ImageView;

import com.example.mybaiduweishi.MyApplication;
import com.example.mybaiduweishi.R;
import com.example.mybaiduweishi.activity.base.BaseActivity;
import com.example.mybaiduweishi.fragments.ThirdSosSuccFragment;
import com.example.mybaiduweishi.fragments.firstFragment;
import com.example.mybaiduweishi.fragments.secondFragment;
import com.example.mybaiduweishi.utils.MyUtil;
import com.example.mybaiduweishi.vo.Contacts;

import butterknife.BindView;

public class OneKeySOSActivity extends BaseActivity {
    firstFragment first;
    secondFragment second;
    ThirdSosSuccFragment thirdSosSuccFragment;
    @BindView(R.id.layout_onekey_help_toolbar)
    Toolbar layoutOnekeyHelpToolbar;
    @BindView(R.id.fl_sos)
    FrameLayout flSos;
//    @BindView(R.id.iv_animation)
//    ImageView imageView;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        layoutOnekeyHelpToolbar.setTitle("一键求救");
        setSupportActionBar(layoutOnekeyHelpToolbar);
        ActionBar actionBar = getSupportActionBar();
        actionBar.setDisplayHomeAsUpEnabled(true);

    }

    @Override
    public int getContentXML() {
        return R.layout.activity_one_key_sos;
    }

    @Override
    public void initData() {

    }

    @Override
    public void initView() {
        //加载Fragment
        if (first == null) {
            first = new firstFragment();
        }
        getFragmentManager().beginTransaction()
                .replace(R.id.fl_sos, first).addToBackStack("SOS").commit();

    }

    public void toSecondFragment() {
        if (second == null) {
            second = new secondFragment();
        }
        getFragmentManager().beginTransaction().hide(first).add(R.id.fl_sos, second).addToBackStack("SOS").commit();

    }

    public void toThirdFragment() {
        /**
         * 电话号码存入数据库
         * 电话内容放共享首选项
         *
        * */
        SharedPreferences sharedPreferences = MyUtil.getSharedPre();
//        String sosInfo = sharedPreferences.getString("sos",null);
        SharedPreferences.Editor editor = sharedPreferences.edit();
        editor.putString("sos","我遇到危险，请求帮助！！！！！！（此乃测试~请勿当真）");
        editor.commit();

        //将求救电话放入数据库，并不是增加数据，而是对已有数据库的表增加一列
        for (int i = 0;i< MyApplication.getSoslist().size() ; i++){
            Contacts c=MyApplication.getSoslist().get(i);
            //比较电话号码，有就加，没有就不加
//            if (MyUtil.getAllContacts().contains())
         }

        //跳转
        if (thirdSosSuccFragment == null) {
            thirdSosSuccFragment = new ThirdSosSuccFragment();
        }
        getFragmentManager().popBackStack("sos", FragmentManager.POP_BACK_STACK_INCLUSIVE);
        getFragmentManager().beginTransaction().hide(second).add(R.id.fl_sos, thirdSosSuccFragment).addToBackStack("Sos").commit();
    }

    public void onBackPressed() {
        if (getSupportFragmentManager().getBackStackEntryCount() > 1) {
            getSupportFragmentManager().popBackStack();
        } else {
            finish();
        }
    }
//
//    @Override
//    public void onWindowFocusChanged(boolean hasFocus) {
//        super.onWindowFocusChanged(hasFocus);
//        AnimationDrawable drawable = (AnimationDrawable) imageView.getBackground();
//        drawable.start();
//    }
}

